﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WF_Loadout_Editor
{
    public class WeaponMod : BaseMod
    {
        public enum Stat
        {
            Damage = 0,
            FireDamage,         // 1
            FreezeDamage,       // 2
            ElectricDamage,     // 3
            ArmorPierceDamage,  // 4
            Puncture,           // 5
            MultiShot,          // 6
            Stun,               // 7
            CriticalChance,     // 8
            CriticalDamage,     // 9
            FireRate,           // 10
            ReloadSpeed,        // 11
            MagazineCapacity,   // 12
            AmmoCapacity,       // 13
            ChargeDamage,       // 14
            ChargeSpeed,        // 15
        }

        private Stat statType;
        public Stat StatType
        {
            get { return statType; }
            set { statType = value; }
        }

        public WeaponMod()
        {

        }

        public WeaponMod(string name1, string description1, ModType type1, Polarity polarityType1, float statModifier1, float currentStat1, int currentRank1, int maxRank1, int baseCost1, int statType1)
            : base(name1, description1, type1, polarityType1, statModifier1, currentStat1, currentRank1, maxRank1, baseCost1)
        {
            statType = (Stat)statType1;
            string fileName = "Resources/Weapon Mods/";
            string fileExt = ".png";
            fileName += name1;
            fileName = fileName + fileExt;
            if (Name != "Default")
                Picture = Image.FromFile(fileName);
        }

        public override Object ModifyStat(Object obj)
        {
            // modifies a weapon stat based on the mod
            Weapon weapon = (Weapon) obj;
            switch (statType)
            {
                case Stat.Damage:
                    // new normal_dmg = (normal_dmg + (normal_dmg * normal_mod)) * enemy_normal_res
                    weapon.Damage = weapon.BaseDamage + (weapon.BaseDamage * (this.CurrentStat / 100.0f));
                    break;
                case Stat.FireDamage:
                    // (normal_dmg * ele_mod) * enemy_res_ele = ele_dmg 
                    weapon.FireDamage = this.CurrentStat;
                    break;
                case Stat.FreezeDamage:
                    // (normal_dmg * ele_mod) * enemy_res_ele = ele_dmg 
                    weapon.FireDamage = this.CurrentStat;
                    break;
                case Stat.ElectricDamage:
                    // (normal_dmg * ele_mod) * enemy_res_ele = ele_dmg 
                    weapon.FireDamage = this.CurrentStat;
                    break;
                case Stat.ArmorPierceDamage:
                    // (normal_dmg * ele_mod) * enemy_res_ele = ele_dmg 
                    weapon.FireDamage = this.CurrentStat;
                    break;
                case Stat.Puncture:
                    // puncture + puncture_mod = new puncture
                    weapon.Puncture = this.CurrentStat;
                    break;
                case Stat.MultiShot:
                    // (Rifles and guns)multishot_percentage = 1.0 * multishot_mod
                    // (Shotguns)new num_pellets = num_pellets + (num_pellets * multishot_mod)
                    weapon.MultiShot = this.CurrentStat;
                    break;
                case Stat.Stun:
                    // new stun = stun + stun_mod
                    weapon.Stun = this.CurrentStat;
                    break;
                case Stat.CriticalChance:
                    // new crit_chance = crit_chance + (crit_chance * crit_mod)
                    weapon.CriticalChance = weapon.BaseCriticalChance + (weapon.BaseCriticalChance * (this.CurrentStat / 100.0f));
                    break;
                case Stat.CriticalDamage:
                    // new_base_dmg = any_dmg + (any_dmg * crit_dmg_mod)
                    // crit_dmg_unmodified = new_base_dmg + (new_base_dmg * crit_dmg_weapon)
                    // crit_dmg = crit_dmg_unmodified * enemy_res
                    weapon.CriticalDamage = weapon.BaseCriticalDamage + (weapon.BaseCriticalDamage * this.CurrentStat);
                    break;
                case Stat.FireRate:
                    // new fire_rate = fire_rate / (1 + firerate_mod)
                    weapon.FireRate = weapon.BaseFireRate * (1.0f + (this.CurrentStat / 100.0f));
                    break;
                case Stat.ReloadSpeed:
                    // new reload_speed = reload_speed / (1 + reload_mod)
                    weapon.ReloadSpeed = weapon.BaseReloadSpeed / (1.0f + (this.CurrentStat / 100.0f));
                    break;
                case Stat.AmmoCapacity:
                    // new ammo_max = ammo_max + (ammo_max * ammo_mod) 
                    weapon.AmmoCapacity = weapon.BaseAmmoCapacity + (int)((float)weapon.BaseAmmoCapacity * (this.CurrentStat / 100.0f));
                    break;
                case Stat.MagazineCapacity:
                    // new magazine_size = magazine_size + (magazine_size * magazine_mod)
                    weapon.MagazineCapacity = weapon.BaseMagazineCapacity + (int)((float)weapon.BaseMagazineCapacity * (this.CurrentStat / 100.0f));
                    break;
                case Stat.ChargeDamage:
                    // new charge_dmg = charge_dmg + (charge_dmg * charge_mod)
                    weapon.ChargeDamage = weapon.BaseChargeDamage + (weapon.BaseChargeDamage * this.CurrentStat);
                    break;
                case Stat.ChargeSpeed:
                    // new charge_speed = charge_speed / (1 + chargespeed_mod)
                    weapon.ChargeSpeed = weapon.BaseChargeSpeed / (1.0f + (this.CurrentStat / 100.0f));
                    break;
                default:
                    break;
            }
            return obj;
        }
    }
}
